package com.lucenly.card.base;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.annotation.IdRes;
import android.support.annotation.LayoutRes;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.WindowManager;

import com.lucenly.card.R;
import com.umeng.analytics.MobclickAgent;
import com.vise.xsnow.event.BusManager;
import com.yanzhenjie.sofia.Sofia;

import butterknife.ButterKnife;


/**
 * @Description: Activity基类
 * @author: <a href="http://www.xiaoyaoyou1212.com">DAWI</a>
 * @date: 2016-12-19 14:51
 */
public abstract class BaseActivity extends AppCompatActivity implements View.OnClickListener {

    protected Context mContext;
    private int type = 0;
    LoginBroadCastReciver loginReciver;
    TokenBroadCastReciver tokenReciver;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
        mContext = this;
        if (isRegisterEvent()) {
            BusManager.getBus().register(this);
        }
        ActivityManager.getInstance().addActivity(this);
        loginReciver = new LoginBroadCastReciver();
        registerReceiver(loginReciver,new IntentFilter("LOGIN"));
        tokenReciver = new TokenBroadCastReciver();
        registerReceiver(tokenReciver,new IntentFilter("refreshToken"));
    }

    public int getType() {
        return type;
    }

    public void setType(int type) {
        this.type = type;
    }

    @Override
    public void setContentView(@LayoutRes int layoutResID) {
        super.setContentView(layoutResID);
        if(getType()==0){
            Sofia.with(this).statusBarBackground(getResources().getColor(R.color.color_transparent)).invasionStatusBar().statusBarLightFont();
        }
        ButterKnife.bind(this);
        initView();
        bindEvent();
        initData();
    }

    @Override
    public void setContentView(View view) {
        super.setContentView(view);
        ButterKnife.bind(this);
        initView();
        bindEvent();
        initData();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (isRegisterEvent()) {
            BusManager.getBus().unregister(this);
        }
        ActivityManager.getInstance().removeActivity(this);
        if(loginReciver!=null){
            unregisterReceiver(loginReciver);
        }
        if(tokenReciver!=null){
            unregisterReceiver(tokenReciver);
        }
    }
    @Override
    public void onClick(View view) {
        processClick(view);
    }

    protected <E extends View> E F(@IdRes int viewId) {
        return (E) super.findViewById(viewId);
    }

    protected <E extends View> E F(@NonNull View view, @IdRes int viewId) {
        return (E) view.findViewById(viewId);
    }

    protected <E extends View> void C(@NonNull E view) {
        view.setOnClickListener(this);
    }

    protected boolean isRegisterEvent() {
        return false;
    }

    /**
     * 初始化子View
     */
    protected abstract void initView();

    /**
     * 绑定事件
     */
    protected abstract void bindEvent();

    /**
     * 初始化数据
     */
    protected abstract void initData();

    /**
     * 点击事件处理
     *
     * @param view
     */
    protected abstract void processClick(View view);

    private class LoginBroadCastReciver extends BroadcastReceiver{

        @Override
        public void onReceive(Context context, Intent intent) {
//            Intent intent2 = new Intent(BaseActivity.this, LoginActivity.class);
//            intent2.putExtra("type",1);
//            startActivity(intent2);
        }
    }
    public void onResume() {
        super.onResume();
        MobclickAgent.onResume(this); //统计时长
    }
    public void onPause() {
        super.onPause();
        MobclickAgent.onPause(this); //统计时长
    }
    private class TokenBroadCastReciver extends BroadcastReceiver{

        @Override
        public void onReceive(Context context, Intent intent) {
            initData();
        }
    }
}
